package shapes;

import interfaces.IIterator;
import interfaces.IShapeCreator;

import java.awt.Graphics;

import utilities.ShapeNodeListSorter;

import nodes.ShapeNode;
import nodes.ShapeNodeList;

public class ShapeHandler {
	
	private ShapeNodeList originalList;
	//private ShapeNodeList sortedList;
	private IShapeCreator shapeCreator;
	private AbstractShape shape;
	private ShapeNodeListSorter sorter;
	
	public ShapeHandler(int size) {
		originalList = new ShapeNodeList(size);
        //sortedList = new ShapeNodeList(size);
	}
	
	public void addShape(String shapeInfo) {
		shapeCreator = new ShapeCreator();
		shapeCreator = new ShapeCreatorDecorator(shapeCreator);
		
		shape = shapeCreator.createShape(shapeInfo);
		
		originalList.add(shape);
        //sortedList.add(shape);
	}
	
	public void draw(Graphics graphics) {
			
		//IIterator iterator = sortedList.createIteratorFromFirst();
        IIterator iterator = originalList.createIteratorFromFirst();
        
        ShapeNode node;
		
		while(!iterator.isNull()) {
			node = new ShapeNode(shape);
			node = (ShapeNode)iterator.next();
			//node.setData(new IShapeDecorator(node.getData()));
			node.getData().draw(graphics);
			System.out.println(node.getData().getShapeID());
			//node.setData(((IShapeDecorator) node.getData()).getShape());
		}
		System.out.println("END OF areas");
	}
	
	public boolean isEmpty() {
		return originalList.isEmpty();
	}
	
	public void sortByIDasc(){
		if (!isEmpty()){
		sorter = new ShapeNodeListSorter(originalList);
        originalList = sorter.sortByShapeIDAscending();
        originalList.showNewOrder();
		}
	}
	
	public void sortByIDdesc(){	
		if (!isEmpty()){
			sorter = new ShapeNodeListSorter(originalList);
            originalList = sorter.sortByShapeIDDescending();
            originalList.showNewOrder();
		}		
	}
	
	public void sortByAreaAsc(){
		if (!isEmpty()){
		sorter = new ShapeNodeListSorter(originalList);
        originalList = sorter.sortByAreaAscending();
        originalList.showNewOrder();
		}
	}
	
	public void sortByAreaDesc(){	
		if (!isEmpty()){
			sorter = new ShapeNodeListSorter(originalList);
            originalList = sorter.sortByAreaDescending();
            originalList.showNewOrder();
		}		
	}
	
	public void sortByFormeType1(){
		if (!isEmpty()){
		sorter = new ShapeNodeListSorter(originalList);
        originalList = sorter.sortByFormeType1();
        originalList.showNewOrder();
		}
	}
	
	public void sortByFormeType2(){	
		if (!isEmpty()){
			sorter = new ShapeNodeListSorter(originalList);
            originalList = sorter.sortByFormeType2();
            originalList.showNewOrder();
		}		
	}
	
	public void sortByDistance(){	
		if (!isEmpty()){
			sorter = new ShapeNodeListSorter(originalList);
			originalList = sorter.sortByDistance();
            originalList.showNewOrder();
		}		
	}
    
    public void getOriginalList() throws CloneNotSupportedException{	
        originalList.setOriginal();
	}

}